package gui;

import dataconnect.SqlConnection;

import javax.swing.*;
import java.awt.*;

/**
 * description: MainPage
 * 主界面
 * @author what-is-me
 * @date 2022/1/3 23:33
 */
public class MainPage extends JPanel {
    JPanel p1, p2, p3, p4, p5, p;

    public MainPage(SqlConnection sql) {
        /**
         * Description: MainPage
         * @param sql:dataconnect.SqlConnection
         * @return {@link null}
         * @throws
         * @author what-is-me
         * @date 2022/1/3 23:33
         */
        super(new BorderLayout());
        p1 = new AllInvestment(sql);
        p2 = new Query("byyear");
        p3 = new Query("byname");
        p4 = new AddItem();
        p5 = new JPanel(new BorderLayout());
        JLabel title = new JLabel("已完成项目盈利", JLabel.CENTER);
        title.setFont(new Font("宋体", Font.BOLD, 20));
        p5.add(title, BorderLayout.NORTH);
        p5.add(new Earnings(sql), BorderLayout.CENTER);
        p = p1;
        add(p, BorderLayout.CENTER);
        JMenuBar menuBar = new JMenuBar();
        add(menuBar, BorderLayout.NORTH);
        JMenu all = new JMenu("全部");
        JMenu query = new JMenu("搜索");
        JMenu addin = new JMenu("添加");
        JMenu earning = new JMenu("盈利");
        JMenuItem add = new JMenuItem("添加投资"), earn = new JMenuItem("已完成收益");
        JMenuItem fin = new JMenuItem("已结束"), r = new JMenuItem("进行中");
        addin.add(add);
        earning.add(earn);
        all.add(fin);
        all.add(r);
        menuBar.add(all);
        menuBar.add(query);
        menuBar.add(addin);
        menuBar.add(earning);
        JMenuItem byy = new JMenuItem("按年份");
        JMenuItem byn = new JMenuItem("按项目名");
        query.add(byy);
        query.add(byn);
        fin.addActionListener(e -> {
            update(p1);
            p1.setLocation(1, 0);
        });
        r.addActionListener(e -> {
            update(p1);
            p1.setLocation(2, 0);
        });
        byy.addActionListener(e -> update(p2));
        byn.addActionListener(e -> update(p3));
        add.addActionListener(e -> update(p4));
        earn.addActionListener(e -> update(p5));
    }

    void update(JPanel px) {
        /**
         * Description: update
         * 更换页面
         * @param px:javax.swing.JPanel
         * @return
         * @throws
         * @author what-is-me
         * @date 2022/1/3 23:34
         */
        remove(p);
        add(px, BorderLayout.CENTER);
        p = px;
        updateUI();
    }
}